package com.yhyvr.api.controller.store;

import com.alibaba.fastjson.JSON;
import com.lz.kit.LogKit;
import com.lz.kit.ehcache.CacheKit;
import com.lz.mybatis.jdbc.auto.dao.BoProxyMapper;
import com.lz.tool.des.Des;
import com.yhyvr.api.handle.ApiData;
import com.yhyvr.api.service.store.ApiProxyService;
import com.yhyvr.api.tool.ViewFactory;
import com.yhyvr.constant.CommonConstant;
import com.yhyvr.constant.WxConstant;
import com.yhyvr.pojo.ApiConstant;
import com.yhyvr.pojo.req.api.ApiReqBody;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.store.user.*;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.user.login.LoginResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by tech6 on 2016/10/11.
 */

@Controller
public class ApiUserController{
    @Resource
    private ApiProxyService apiProxyService;
    @Resource
    private BoProxyMapper boProxyMapper;
    @RequestMapping("/api/store/login/cookie/{cookie}")
    public String cookie(@PathVariable String cookie, HttpServletResponse response) {
        if (CacheKit.get("wxloginTokenEx", cookie) != null) {
            Cookie c = null;
            try {
                c = new Cookie(ApiConstant.COOKIE_FENXIAO_USER, Des.encode(ApiConstant.COOKIE_FENXIAO_USER_DESP0,cookie));
            } catch (Exception e) {
                e.printStackTrace();
            }
            c.setMaxAge(ApiConstant.cookieMaxAge);
            c.setPath("/");
            response.addCookie(c);
        } else {
            return "/store/login";
        }
        return "/store/index";
    }
    @RequestMapping("/api/store/login/wx")
    public String login(){
        return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ WxConstant.appId +"&redirect_uri=https%3A%2F%2F"+ CommonConstant.getInstance().getApiDomain()+"%2Fapi%2Fstore%2Flogin%2Fwx%2Fcalback&response_type=code&scope=snsapi_userinfo&state=mall#wechat_redirect";
    }

    @RequestMapping("/api/store/login/wx/calback")
    public String wxLogin(String code, HttpServletResponse response){
        String c="";
        ApiRespData<LoginResp> loginRespApiRespData = apiProxyService.threadLogin(code);
        LogKit.info("登录信息"+ JSON.toJSONString(loginRespApiRespData));
        if (loginRespApiRespData.getHead().getCode() == 1) {
            LoginResp body = loginRespApiRespData.getBody();
                c=body.getUserId();
                CacheKit.put("wxloginTokenEx",c,c);
            return "redirect:/api/store/login/cookie/"+c;
        }else{
            return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ WxConstant.appId +"&redirect_uri=https%3A%2F%2F"+CommonConstant.getInstance().getApiDomain()+"%2Fapi%2Fstore%2Flogin%2Fwx%2Fcalback&response_type=code&scope=snsapi_userinfo&state=mall#wechat_redirect";
        }

    }

    /**
     * 注册
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/register")
    public @ResponseBody ModelAndView register(@ApiData(tokenValidate = false) ApiReqData<ApiStoreRegisterReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiProxyService.register(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 修改绑定手机号
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/phone/bind")
    public @ResponseBody ModelAndView bindPhone(@ApiData(tokenValidate = false) ApiReqData<ApiStoreBindPhoneReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiProxyService.bindPhone(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 用户信息获取
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/proxy/info")
    public @ResponseBody ModelAndView info(@ApiData(tokenValidate = false) ApiReqData<ApiReqBody> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiProxyService.userInfo(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 用户信息更新
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/proxy/info/update")
    public @ResponseBody ModelAndView update(@ApiData(tokenValidate = false) ApiReqData<ApiUpdateUserInfoReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiProxyService.updateUserInfo(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 修改密码
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/user/password/update")
    public @ResponseBody ModelAndView updatePassWord(@ApiData(tokenValidate = false) ApiReqData<ApiUserPassWordUpdateReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiProxyService.updatePassword(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }
    /**
     * 手机号登陆
     * @param loginReq
     */
    @RequestMapping("/api/store/login")
    public @ResponseBody ModelAndView login(@ApiData(tokenValidate = false) ApiReqData<LoginReq> loginReq){
            ApiRespData<LoginResp> loginRespApiRespData = apiProxyService.login(loginReq);
            if (loginRespApiRespData.getHead().getCode() == 1) {
                LoginResp body = loginRespApiRespData.getBody();
                Cookie cookie = null;
                try {
                    cookie = new Cookie(ApiConstant.COOKIE_FENXIAO_USER, Des.encode(ApiConstant.COOKIE_FENXIAO_USER_DESP0,body.getUserId()));
                } catch (Exception e) {
                    return ViewFactory.buildApiJsonpView(ApiRespData.buildFail(), loginReq.getCrossDomain());
                }
                cookie.setMaxAge(ApiConstant.cookieMaxAge);
                cookie.setPath("/");
                loginReq.getHead().getHttpServletResponse().addCookie(cookie);
                return ViewFactory.buildApiJsonpView(ApiRespData.buildSucc(), loginReq.getCrossDomain());
            } else {
                return ViewFactory.buildApiJsonpView(loginRespApiRespData, loginReq.getCrossDomain());
            }

    }

    /**
     * 验证码登陆
     * @param loginReq
     */
    @RequestMapping("/api/store/login/code")
    public @ResponseBody ModelAndView loginByCode(@ApiData(tokenValidate = false) ApiReqData<PhpneCodeLoginReq> loginReq){
        ApiRespData<LoginResp> loginRespApiRespData = apiProxyService.loginByCode(loginReq);
            if (loginRespApiRespData.getHead().getCode() == 1) {
                LoginResp body = loginRespApiRespData.getBody();
                Cookie cookie = null;
                try {
                    cookie = new Cookie(ApiConstant.COOKIE_FENXIAO_USER, Des.encode(ApiConstant.COOKIE_FENXIAO_USER_DESP0,body.getUserId()));
                } catch (Exception e) {
                    return ViewFactory.buildApiJsonpView(ApiRespData.buildFail(), loginReq.getCrossDomain());
                }
                cookie.setMaxAge(ApiConstant.cookieMaxAge);
                cookie.setPath("/");
                loginReq.getHead().getHttpServletResponse().addCookie(cookie);
                return ViewFactory.buildApiJsonpView(ApiRespData.buildSucc(), loginReq.getCrossDomain());
            } else {
                return ViewFactory.buildApiJsonpView(loginRespApiRespData, loginReq.getCrossDomain());
        }

    }



    /**
     * 品鉴师申请
     * @param apiReqDataApiReqData
     */

    @RequestMapping("/api/store/taster/apply")
    public @ResponseBody ModelAndView applyTaster(@ApiData(tokenValidate = false) ApiReqData<ApiTasterApplyReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiProxyService.applyTaster(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }


    /**
     * 进入试吃系统
     * @param apiReqDataApiReqData
     */

    @RequestMapping("/api/store/taster/system")
    public @ResponseBody ModelAndView  toTasterSystem(@ApiData(tokenValidate = false) ApiReqData<ApiTasterApplyReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiProxyService.toTasterSystem(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }
    /**
     * 意见反馈
     * @param apiAntiFakeValidataReqApiReqData
     */
    @RequestMapping("/api/school/feedback/add")
    public @ResponseBody ModelAndView proxyApplication(@ApiData(tokenValidate = false) ApiReqData<ApiFeedBackReq> apiAntiFakeValidataReqApiReqData){
        return ViewFactory.buildApiJsonpView(apiProxyService.feedBack(apiAntiFakeValidataReqApiReqData),apiAntiFakeValidataReqApiReqData.getCrossDomain());
    }

}
